<!--
 * @Author         : yanyongyu
 * @Date           : 2022-10-03 08:50:11
 * @LastEditors    : yanyongyu
 * @LastEditTime   : 2022-10-04 03:43:09
 * @Description    : None
 * @GitHub         : https://github.com/yanyongyu
-->

{% set GRAPH_WIDTH = 5 %}

{% macro diff_stat(file, detailed=false) %}
<span class="diffstat">
  {% if file.is_binary_file %}
  BIN
  {% else %}

  {% set changed = file.added + file.removed %}
  {% if detailed %}
  <span class="color-fg-success">+{{ file.added }}</span>
  <span class="color-fg-danger">-{{ file.removed }}</span>
  {% else %}
  {{ changed }}
  {% endif %}

  {# Reference: https://github.com/git/git/blob/bcd6bc478adc4951d57ec597c44b12ee74bc88fb/diff.c#L2791-L2806 #}
  {% set added = file.added %}
  {% set removed = file.removed %}
  {% if GRAPH_WIDTH <= changed %}

  {% set total = scale_linear(changed, GRAPH_WIDTH, changed) %}
  {% if total < 2 and added and removed %}
  {% set total = 2 %}
  {% endif %}

  {% if added < removed %}
  {% set added = scale_linear(added, GRAPH_WIDTH, changed) %}
  {% set removed = total - added %}
  {% else %}
  {% set removed = scale_linear(removed, GRAPH_WIDTH, changed) %}
  {% set added = total - removed %}
  {% endif %}

  {% endif %}

  {# must in one line, no space #}
  {% for _ in range(added) %}<span class="diffstat-block-added"></span>{% endfor %}
  {% for _ in range(removed) %}<span class="diffstat-block-deleted"></span>{% endfor %}
  {% for _ in range(GRAPH_WIDTH - added - removed) %}<span class="diffstat-block-neutral"></span>{% endfor %}

  {% endif %}
</span>
{% endmacro %}
